<?php

/**
 * This is the model class for table "{{punish}}".
 *
 * The followings are the available columns in table '{{punish}}':
 * @property integer $id
 * @property integer $uid
 * @property string $username
 * @property integer $good_id
 * @property string $goods_name
 * @property integer $type
 * @property string $cause
 * @property integer $status
 * @property integer $admin_id
 */
class PunishModel extends BaseModel
{


	CONST STATUS_NOT = 0;
	CONST STATUS_HAS = 1;
	CONST STATUS_CANCEL = 2;//已忽略



	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{punish}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('good_id, goods_name, type', 'required'),
			array('uid, good_id, status, admin_id', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>50),
			array('goods_name', 'length', 'max'=>500),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, uid, username, cause, status, admin_id,add_time,reason', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'GoodsModel' => array(self::BELONGS_TO, 'GoodsModel','good_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'uid' => '用户ID',
			'username' => '用户名',
			'good_id' => '商品ID',
			'goods_name' => '商品名称',
			'type' => '举报原因类型',
			'cause' => '其它原因',
			'status' => '处理状态',
			'reason' => '处理原因',
			'admin_id' => '处理人',
			'add_time' => '举报时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('good_id',$this->good_id);
		$criteria->compare('goods_name',$this->goods_name,true);
		$criteria->compare('type',$this->type);
		$criteria->compare('cause',$this->cause,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('admin_id',$this->admin_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return PunishModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * 创建搜索条件
	 * @return [type] [description]
	 */
	public function createSearchCriteria()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);
		$criteria->compare('t.uid',$this->uid);
		$criteria->compare('t.username',$this->username,true);
		$criteria->compare('t.good_id',$this->good_id);
		$criteria->compare('t.goods_name',$this->goods_name,true);
		$criteria->compare('t.type',$this->type);
		$criteria->compare('t.cause',$this->cause,true);
		$criteria->compare('t.status',$this->status);
		$criteria->compare('t.admin_id',$this->admin_id);


		return 	$criteria;	
	}

	public static function getStatusHtmlList()
	{
		return array(
			self::STATUS_NOT => '未处理',
			self::STATUS_HAS => '已处理',
			self::STATUS_CANCEL => '已取消',
		);
	}

	public static function getStatusHtmlDetail($data)
	{
		$arrData = self::getStatusHtmlList();
		return  $arrData[$data];
	}

	public static function getTypeHtmlList()
	{
		return array(
			1 => '商品已卖光',
			2 => '抢购提前开始',
			3 => '商品链接不正确',
			4 => '商品分类不正确',
			5 => '价格与网站不一致(VIP折扣登录淘宝后才能看到，请登录后查看价格是否一致)',
			6 => '商品描述有误',
			7 => '其他原因',
		);
	}

	public static function getTypeHtmlDetail($data)
	{
		$arrData = self::getTypeHtmlList();
		$data = explode(',', $data);
		$html = '';
		foreach ($data as $value) 
		{
			if($value == 7)
			{
				$html .= $arrData[$value] . ':';
			}else
			{
				$html .= $arrData[$value] . '</br>';				
			}
		}
		return  $html;
	}

	protected function beforeSave()
	{

        if (!is_numeric($this->add_time)) 
        {
            $this->add_time = strtotime($this->add_time);
        }
		
		if(is_array($this->type))
		{
			$this->type = implode(',', $this->type);
		}      

		if ($this->isNewRecord)
		{
			$this->add_time = time();
			$this->status = 0;
			$this->admin_id = 0;		
			$this->cause = CHtml::encode($this->cause);
		}

		$this->reason = CHtml::encode($this->reason);
		
		return true;
	}

    /**
     * @param [type] $arrData 增加数据
     * @param [type] $objGoodsModel 商品模型 
     * @param [type] $arrUserinfo 用户信息 
     */
    public function editor($arrData, $objGoodsModel, $arrUserinfo = '')
    {
    	empty($arrUserinfo) ? $arrUserinfo = yii::app()->user->getInfo() : '';
		$arrData['uid'] = !$arrUserinfo ? 0 : $arrUserinfo['uid'];
		$arrData['username'] = !$arrUserinfo ? '' : $arrUserinfo['username'];
		$arrData['good_id'] = $objGoodsModel->goods_id;			
		$arrData['goods_name'] = $objGoodsModel->goods_name;	        	        
        //赋值
        $this->attributes = $arrData;
        return $this->save();
    }	
}
